//
//  commendTableViewCell.m
//  TinyFortune
//
//  Created by lc on 16/5/31.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "commendTableViewCell.h"
#import "commendModel.h"
//头像
//评论
//用户名
//时间
@implementation commendTableViewCell{
    UIImageView *_iconImageView;//头像
    UILabel *_commendLabel;//评论
    UILabel *_nameLabel;//用户姓名
    UILabel *_timeLabel;//时间
    UIView *_lineView;//华丽的分割线
}
#pragma mark - 实例化子视图,并且添加到cell上
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //头像
        _iconImageView = [UIImageView new];
        [self.contentView addSubview:_iconImageView];
        _iconImageView.layer.cornerRadius = 35;
        _iconImageView.layer.masksToBounds = YES ;
        //评论
        _commendLabel = [UILabel new];
        [self.contentView addSubview:_commendLabel];
        _commendLabel.font = [UIFont systemFontOfSize:14];
        _commendLabel.numberOfLines = 0; //自动换行
        //作者名
        _nameLabel = [UILabel new];
        [self.contentView addSubview:_nameLabel];
        _nameLabel.font = [UIFont boldSystemFontOfSize:17];
        //时间
        _timeLabel = [UILabel new];
        [self.contentView addSubview:_timeLabel];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        //华丽的分割线
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat margin = 10 ;
    //头像
    
    CGFloat iconX = margin;
    CGFloat iconY = margin ;
    CGFloat iconW = 70 ;
    CGFloat iconH = iconW ;
    _iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
   
    //名字
    CGFloat nameX = iconW + iconX + margin ;
    CGFloat nameY = iconY + margin ;
    CGRect nameRect = [_nameLabel.text boundingRectWithSize:CGSizeMake(LC_WIDTH, LC_HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]} context:nil];
    _nameLabel.frame = CGRectMake(nameX, nameY, nameRect.size.width, nameRect.size.height);
    //华丽的分割线
    CGFloat lineX = nameX ;
    CGFloat lineY = 89;
    CGFloat lineW = LC_WIDTH - iconW - margin * 3;
    CGFloat lineH = 1;
    _lineView.frame = CGRectMake(lineX, lineY, lineW, lineH);
    //评论
    CGFloat commendX = nameX ;
    CGFloat commendY = nameY + margin *3 ;
    CGRect commendRect = [_commendLabel.text boundingRectWithSize:CGSizeMake(LC_WIDTH - commendX, LC_HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}  context:nil];
    _commendLabel.frame = CGRectMake(commendX, commendY, commendRect.size.width, commendRect.size.height);
    //时间
    CGFloat timeX = LC_WIDTH - 80  - margin;
    CGFloat timeY = margin ;
    CGFloat timeW = 80 ;
    CGFloat timeH = 20;
    _timeLabel.frame = CGRectMake(timeX, timeY, timeW, timeH);
    
  
        
}
#pragma mark - 给子视图赋值
- (void)setModel:(commendModel *)model{
    _model = model ;
    //头像
    [_iconImageView sd_setImageWithURL:[NSURL URLWithString:_model.icon] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
    //用户名
    _nameLabel.text = _model.name ;
    //评论
    _commendLabel.text = _model.content;
    //时间
    _timeLabel.text = _model.time ;
}
@end
